home *** CD-ROM | disk | FTP | other *** search
/ Programming in Microsoft Windows with C# / Programacion en Microsoft Windows con C#.iso / Original Code / Printing / ImagePrint / ImagePrint.cs next >
Encoding:
Text File  |  2001-01-15  |  4.2 KB  |  119 lines

  1. //-----------------------------------------
  2. // ImagePrint.cs ⌐ 2001 by Charles Petzold
  3. //-----------------------------------------
  4. using System;
  5. using System.Drawing;
  6. using System.Drawing.Printing;
  7. using System.Windows.Forms;
  8.  
  9. class ImagePrint: ImageIO
  10. {
  11.      PrintDocument   prndoc = new PrintDocument();
  12.      PageSetupDialog setdlg = new PageSetupDialog();
  13.      PrintDialog     prndlg = new PrintDialog();
  14.  
  15.      MenuItem miFileSet, miFilePrint, miFileProps;
  16.  
  17.      public new static void Main()
  18.      {
  19.           Application.Run(new ImagePrint());
  20.      }
  21.      public ImagePrint()
  22.      {
  23.           Text = strProgName = "Image Print";
  24.  
  25.                // Initialize PrintDocument and common dialog boxes.
  26.  
  27.           prndoc.PrintPage += new PrintPageEventHandler(OnPrintPage);
  28.           setdlg.Document = prndoc;
  29.           prndlg.Document = prndoc;
  30.  
  31.                // Add menu items.
  32.  
  33.           Menu.MenuItems[0].Popup += new EventHandler(MenuFileOnPopup);
  34.           Menu.MenuItems[0].MenuItems.Add("-");
  35.  
  36.                // File Page Setup item
  37.  
  38.           miFileSet = new MenuItem("Page Set&up...");
  39.           miFileSet.Click += new EventHandler(MenuFileSetupOnClick);
  40.           Menu.MenuItems[0].MenuItems.Add(miFileSet);
  41.  
  42.                // File Print item
  43.  
  44.           miFilePrint = new MenuItem("&Print...");
  45.           miFilePrint.Click += new EventHandler(MenuFilePrintOnClick);
  46.           miFilePrint.Shortcut = Shortcut.CtrlP;
  47.           Menu.MenuItems[0].MenuItems.Add(miFilePrint);
  48.           Menu.MenuItems[0].MenuItems.Add("-");
  49.  
  50.                // File Properties item
  51.  
  52.           miFileProps = new MenuItem("Propert&ies...");
  53.           miFileProps.Click += new EventHandler(MenuFilePropsOnClick);
  54.           Menu.MenuItems[0].MenuItems.Add(miFileProps);
  55.      }
  56.      protected override void OnPaint(PaintEventArgs pea)
  57.      {
  58.           if (image != null)
  59.                ScaleImageIsotropically(pea.Graphics, image, ClientRectangle);
  60.      }
  61.      void MenuFileOnPopup(object obj, EventArgs ea)
  62.      {
  63.           miFileSet.Enabled = 
  64.           miFilePrint.Enabled = 
  65.           miFileProps.Enabled = (image != null);
  66.      }
  67.      void MenuFileSetupOnClick(object obj, EventArgs ea)
  68.      {
  69.           setdlg.ShowDialog();
  70.      }
  71.      void MenuFilePrintOnClick(object obj, EventArgs ea)
  72.      {
  73.           if (prndlg.ShowDialog() == DialogResult.OK)
  74.           {
  75.                prndoc.DocumentName = Text;
  76.                prndoc.Print();
  77.           }
  78.      }
  79.      void MenuFilePropsOnClick(object obj, EventArgs ea)
  80.      {
  81.           string str = 
  82.                "Size = " + image.Size + 
  83.                "\nHorizontal Resolution = " + image.HorizontalResolution +
  84.                "\nVertical Resolution = " + image.VerticalResolution +
  85.                "\nPhysical Dimension = " + image.PhysicalDimension +
  86.                "\nPixel Format = " + image.PixelFormat;
  87.  
  88.           MessageBox.Show(str, "Image Properties");
  89.      }
  90.      void OnPrintPage(object obj, PrintPageEventArgs ppea)
  91.      {
  92.           Graphics   grfx  = ppea.Graphics;
  93.           RectangleF rectf = new RectangleF(
  94.                ppea.MarginBounds.Left - 
  95.                (ppea.PageBounds.Width - grfx.VisibleClipBounds.Width) / 2,
  96.                ppea.MarginBounds.Top - 
  97.                (ppea.PageBounds.Height - grfx.VisibleClipBounds.Height) / 2,
  98.                ppea.MarginBounds.Width,
  99.                ppea.MarginBounds.Height);
  100.  
  101.           ScaleImageIsotropically(grfx, image, rectf);
  102.      }
  103.      void ScaleImageIsotropically(Graphics grfx, Image image, 
  104.                                   RectangleF rectf)
  105.      {
  106.           SizeF sizef = new SizeF(image.Width / image.HorizontalResolution,
  107.                                   image.Height / image.VerticalResolution);
  108.  
  109.           float fScale = Math.Min(rectf.Width  / sizef.Width,
  110.                                   rectf.Height / sizef.Height);
  111.  
  112.           sizef.Width  *= fScale;
  113.           sizef.Height *= fScale;
  114.           
  115.           grfx.DrawImage(image, rectf.X + (rectf.Width  - sizef.Width ) / 2,
  116.                                 rectf.Y + (rectf.Height - sizef.Height) / 2,
  117.                                 sizef.Width, sizef.Height);
  118.      }
  119. }